package com.limingru.online_class.common.exception;

import com.limingru.online_class.common.Result;
import com.limingru.online_class.common.ResultGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class CustomExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(CommonException.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {

        logger.error("[ 系统异常 ]{}", e);

        if (e instanceof CommonException) {
            CommonException c = (CommonException) e;
            return new Result(c.getCode(), c.getMsg());
        } else {
            return ResultGenerator.generateFailResult("全局异常，未知错误");
        }
    }
}
